package com.milloc.db.common.metadata;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * mybatis获取返回参数，元数据
 *
 * @author gongdeming
 * @create 2019-09-03
 */
@Intercepts({@Signature(
        type = ResultSetHandler.class,
        method = "handleResultSets",
        args = {Statement.class}
)})
public class MetaDataInterceptor implements Interceptor {
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        if (MetaDataHelper.isStart()) {
            Object[] args = invocation.getArgs();
            Statement statement = (Statement) args[0];
            ResultSet resultSet = statement.getResultSet();
            MetaDataHelper.add(resultSet.getMetaData());
        }
        return invocation.proceed();
    }
}
